package com.boot41.controller;

import com.boot41.entity.Product;
import com.boot41.mapper.ProductMapper;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wxd
 * @version 1.0
 */
@RestController
public class ProductController {
    final
    ProductMapper mapper;

    public ProductController(ProductMapper mapper) {
        this.mapper = mapper;
    }

    @RequestMapping("/insert")
    public void insert(@RequestBody Product product) {
        mapper.insert(product);
    }

    @RequestMapping("/select")
    public List<Product> select() {
        /*
        SpringMVC框架当发现返回值类型为集合或自定义的对象类型时
        会将集合或对象转成JSON格式的字符串,然后再将JSON格式字符串转成二进制数据进行网络传输
        [{"id":1,"title":"阿迪袜子","price":10.0,"saleCount":100},{"id":1,"title":"阿迪袜子","price":10.0,"saleCount":100},{"id":1,"title":"阿迪袜子","price":10.0,"saleCount":100}]
         */
        return mapper.select();
    }

    @RequestMapping("/delete")
    public void delete(int id) {
        mapper.deleteById(id);
    }

    @RequestMapping("/selectById")
    public Product selectById(int id) {
        return mapper.selectById(id);
    }

    @RequestMapping("/change")
    public void updateById(@RequestBody Product product) {
        mapper.updateById(product);
    }
}
